package com.ule.activity.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

@Configuration
@PropertySource(value = {"classpath:jdbc2.properties"})
@MapperScan(basePackages = "com.ule.activity.dao", sqlSessionTemplateRef = "oracleSqlSessionTemplate",properties = {"order=BEFORE","mapper.before=true","mapper.order=BEFORE"})
public class DefalutDataSource {
    @Autowired
    private Environment env;

    @Bean(name = "oracleDataSource", destroyMethod = "close", initMethod = "init")
    @Primary
    public DataSource oracleDataSource() {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        dataSource.setUrl(env.getProperty("jdbc.postmall-master.url"));
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUsername(env.getProperty("jdbc.postmall-master.username"));
        dataSource.setPassword(env.getProperty("jdbc.postmall-master.password"));
        dataSource.setInitialSize(Integer.parseInt(env.getProperty("jdbc.postmall-master.initialSize")));
        dataSource.setMaxActive(Integer.parseInt(env.getProperty("jdbc.postmall-master.maxActive")));
        dataSource.setMinIdle(Integer.parseInt(env.getProperty("jdbc.postmall-master.minIdle")));
        dataSource.setMaxWait(Long.parseLong(env.getProperty("jdbc.postmall-master.maxWait")));
        dataSource.setValidationQuery(env.getProperty("SELECT 1 FROM DUAL"));
        dataSource.setName("oracle-druid");
        return dataSource;

    }

    @Bean(name = "oracleSessionFactory")
    @Primary
    public SqlSessionFactory getOracleSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis-mapper/db1/**/*.xml"));
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
        sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.getConfiguration().setLogImpl(Log4j2Impl.class);
        return sqlSessionFactory;
    }

    @Bean(name = "oracleSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("oracleSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    @Bean(name = "oracleTransactional")
    @Primary
    public PlatformTransactionManager oracleTransactional(@Qualifier("oracleDataSource") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

/*    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(@Qualifier("oracleSqlSessionTemplate") SqlSessionTemplate oracleSqlSessionTemplate){
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
//        configurer.setSqlSessionTemplateBeanName("oracleSqlSessionTemplate");
        configurer.setSqlSessionTemplate(oracleSqlSessionTemplate);
        // 修改为自己的mapper包路径
        configurer.setBasePackage("com.ule.activity.dao");
        Properties properties = new Properties();
        // 在新增之前执行序列生成
        properties.setProperty("order", "before");
        configurer.setProperties(properties);
        return configurer;
    }*/
}
